<?php

namespace app\admin\model\post;

use app\helper\Helper;
use think\Exception;
use think\Model;


class Reward extends Model
{

    

    

    // 表名
    protected $table = 'post_reward';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    protected $dateFormat='Y-m-d H:i:s';

    // 追加属性
    protected $append = [

    ];
    

    



    public function reward($post_id,$amount,$uid){

        $post= Post::get($post_id);
        if(empty($post)) throw new Exception('圈子不存在');
        if($amount<1) throw new Exception('最低打赏1元');
        $data=[
            'uid'=>$uid,
            'post_id'=>$post_id,
            'post_uid'=>$post->uid,
            'amount'=>$amount,
        ];

        $reward = self::create($data);
        $reward->order_no=Helper::orderNo('R',$reward->id);
        $reward->save();
        return $reward;

    }



    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'uid', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function post()
    {
        return $this->belongsTo('app\admin\model\Post', 'post_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
